/**
 * 
 */
package com.et114.core.control;

import javax.servlet.ServletException;

import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.beanutils.ConvertUtils;

import org.springframework.util.Assert;

import com.et114.core.utility.ArgumentTokenizer;
/*
 * @author guanhw
 */
public class StrutsConverterPlugin implements PlugIn {
	protected final Log	logger	= LogFactory.getLog ( getClass ( ) );
	private String converters ; 
	/* (non-Javadoc)
	 * @see org.apache.struts.action.PlugIn#destroy()
	 */
	public void destroy ( ) {
		// TODO Auto-generated method stub
	}
	
	/* (non-Javadoc)
	 * @see org.apache.struts.action.PlugIn#init(org.apache.struts.action.ActionServlet, org.apache.struts.config.ModuleConfig)
	 */
	public void init ( ActionServlet arg0 , ModuleConfig arg1 )
			throws ServletException {
		// TODO Auto-generated method stub
		logger.info ( "struts StrutsConverterPlugin plugin init ...." ) ; 
		try {
			if ( converters != null && !"".equals ( converters.trim ( ) ) ) {
				converters = converters.trim ( ) ; 
				ArgumentTokenizer at = new ArgumentTokenizer ( converters );
				while ( at.hasNext ( ) ) {
					String clazz = at.nextToken ( ) ; 
					Object obj = getClass ( ).getClassLoader ( ).loadClass (  clazz  ).newInstance ( ); 
					Assert.isInstanceOf (
							IConverter.class ,
							obj ,
							"IConverter "
									+ obj.getClass ( ).getName ( )
									+ " must implement IConverter" );		
					IConverter ic = ( IConverter  ) obj ; 
					ConvertUtils.register ( ic , ic.getDefaultClass ( ) ) ; 
				}			
			}
		}
		catch ( Exception e ) {
			logger.error ( "struts StrutsConverterPlugin plugin::" + e.getMessage ( ) , e ) ;
		}
		logger.info ( "struts StrutsConverterPlugin plugin init end  ...." ) ; 
	}

	public String getConverters ( ) {
		return converters;
	}

	public void setConverters ( String converters ) {
		this.converters = converters;
	}

}
